﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.IO;
using System.Drawing;
using System.Web.UI.HtmlControls;
public partial class admin_OrderAdd : SitePage
{
    busStoreOrder _busOrder = new busStoreOrder();
    busCustomer _busCus = new busCustomer();
    #region Fields
   

  
    public int OrderId
    {

        get
        {
            object o = ViewState["OrderId"];
            if (o != null) return (int)o;
            return int.MinValue;
        }

        set { ViewState["OrderId"] = value; }

    }
    #endregion

    #region Init,Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.OrderId = clsQueryString.GetInt("OrderId");
            LoadData();
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitData();
    }
    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);

    }
    #endregion

    #region Methods
    private void LoadData()
    {
        if (this.OrderId > 0)
        {
            StoreOrder objOrder = _busOrder.GetByID(this.OrderId);
            if (objOrder != null)
            {
                txtPurchaseOrder.Text = objOrder.PurchaseOrder;
                txtTrackingOrder.Text = objOrder.TrackingOrder;
                drpCustomer.SelectedValue = objOrder.CustomerId.ToString();
                if (objOrder.FinishDate != DateTime.MinValue)
                    txtFinishDate.Text = clsConvert.String(objOrder.FinishDate);
            }
        }
    }
    private void InitData()
    {

        txtTrackingOrder.Text = _busOrder.GenerateTrackingOrderCode();
        LoadCustomer();
    }
    private void LoadCustomer()
    {

        CustomerCollection lstCust = _busCus.GetAll();
        drpCustomer.Items.Clear();
        drpCustomer.Items.Add(new ListItem("- - -", ""));
        foreach (Customer objCus in lstCust)
        {
            drpCustomer.Items.Add(new ListItem(objCus.Name, objCus.CustomerId.ToString()));
        }

    }
    private void UpdateOrder(StoreOrder objOrder)
    {

        objOrder.OrderId = this.OrderId; //''clsQueryString.GetInt("id");
        int result = _busOrder.Update(objOrder);
        if (result == 1)
            Response.Redirect("OrderList.aspx?" + this.GetPageQuery);
        else if (result == -1)//duplicate code
        {
            lblErrorMessage.Text = string.Format(Resources.Resource.MSG_CHECK_EXISTS, "Tracking Order");
        }

        else lblErrorMessage.Text = Resources.Resource.MSG_UPDATE_ERROR;
    }

    private void InsertOrder(StoreOrder objOrder)
    {
        int result = _busOrder.Insert(objOrder);
        if (result > 0)
            Response.Redirect("OrderList.aspx?" + this.GetPageQuery);
        else if (result == -1)//duplicate code
        {
            lblErrorMessage.Text = string.Format(Resources.Resource.MSG_CHECK_EXISTS, "Tracking Order");
        }
        else
        {
            lblErrorMessage.Text = Resources.Resource.MSG_INSERT_ERROR;
        }
    }
    #endregion

    #region Events

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //validate data
            DateTime finishDate = clsConvert.Date(txtFinishDate.Text);
            if (!string.IsNullOrEmpty(txtFinishDate.Text))
            {
                if (finishDate == DateTime.MinValue)
                {
                    lblErrorMessage.Text = string.Format(Resources.Resource.MSG_VALID, "Dự kiến giao hàng");
                    return;
                }
            }
            //get datavalue
            StoreOrder objData = new StoreOrder();
            objData.PurchaseOrder = txtPurchaseOrder.Text.Trim();
            objData.TrackingOrder = txtTrackingOrder.Text.Trim();
            objData.CustomerId = clsConvert.Int(drpCustomer.SelectedValue);
            objData.CreatedBy = this.userloginName;
            objData.CreatedDate = clsDateTime.Now;
            objData.ModifiedBy = this.userloginName;
            objData.FinishDate = finishDate;
            objData.ModifiedDate = clsDateTime.Now;
            if (OrderId > 0)
                UpdateOrder(objData);
            else
                InsertOrder(objData);

        }
        else
        {
            vsumTipNew.ShowMessageBox = false;
            vsumTipNew.ShowSummary = true;
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("OrderList.aspx?" + this.GetPageQuery);
    }

    #endregion

}